package cn.edu.cqvie.netty.protobuf.client;

import cn.edu.cqvie.netty.protobuf.DataInfo;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.concurrent.EventExecutorGroup;

import javax.xml.crypto.Data;
import java.util.Random;

public class TestClientHandler extends SimpleChannelInboundHandler<DataInfo.MyMesage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DataInfo.MyMesage msg) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        int randomInt = new Random().nextInt(3);
        DataInfo.MyMesage myMesage = null;
        if (0 == randomInt) {

            myMesage = DataInfo.MyMesage
                    .newBuilder()
                    .setDataType(DataInfo.MyMesage.DataType.PersonType)
                    .setPerson(DataInfo.Person
                            .newBuilder()
                            .setName("张三")
                            .setAge(20)
                            .setAddress("重庆")
                            .build())
                    .build();


        } else if (1 == randomInt) {
             myMesage = DataInfo.MyMesage
                    .newBuilder()
                    .setDataType(DataInfo.MyMesage.DataType.DogType)
                    .setDog(DataInfo.Dog
                            .newBuilder()
                            .setName("一只狗")
                            .setAge(20)
                            .build())
                    .build();

        } else {
            myMesage = DataInfo.MyMesage
                    .newBuilder()
                    .setDataType(DataInfo.MyMesage.DataType.CatType)
                    .setCat(DataInfo.Cat
                            .newBuilder()
                            .setName("一只猫")
                            .build())
                    .build();
        }
        ctx.channel().writeAndFlush(myMesage);


    }
}
